package com.hongfeng.support.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.item.tag.LogicOperator;
import com.hongfeng.support.model.OrderState;
import com.hongfeng.support.service.OrderStateService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <订单状态Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class OrderStateController extends BaseController {

	@Resource
	private OrderStateService orderStateService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String name, Page<OrderState> page) {
		log.info("状态名：" + name);
		ParmItem item = new ParmItem("name", name, FieldOperator.LIKE);
		orderStateService.findPageByQuery(page, OrderState.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("listAll")
	public void listAll(HttpServletRequest request, HttpServletResponse response) {
		List<OrderState> orderStates = orderStateService.findAll();
		String json = JsonUtils.array2Json(orderStates);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("orderState") String orderStateJson) {
		log.info("添加订单状态:" + (orderStateJson != null ? orderStateJson : null));
		if (orderStateJson == null) {
			this.sendErrorResponse(response, "订单状态不能为空。");
			return;
		}
		OrderState orderState = (OrderState) JsonUtils.json2Object(
				orderStateJson, OrderState.class);
		List<ParmItem> items = new ArrayList<ParmItem>(2);
		items.add(new ParmItem("name", orderState.getName(),
				FieldOperator.EQUAL, LogicOperator.AND));
		items.add(new ParmItem("code", orderState.getCode(),
				FieldOperator.EQUAL, LogicOperator.OR));
		List<OrderState> list = orderStateService.findAllByQuery(
				OrderState.class, items);
		if (list == null || list.size() == 0) {
			orderStateService.create(orderState);
		} else {
			this.sendErrorResponse(response, "该状态名称或编码已存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("orderState") String orderStateJson) {
		log.info("修改订单状态:" + (orderStateJson != null ? orderStateJson : null));
		if (orderStateJson == null) {
			this.sendErrorResponse(response, "订单状态不能为空。");
			return;
		}
		OrderState orderState = (OrderState) JsonUtils.json2Object(
				orderStateJson, OrderState.class);
		OrderState qOrderState = orderStateService.read(orderState.getId());

		boolean nameEquals = orderState.getName().equals(qOrderState.getName());
		boolean codeEquals = orderState.getCode().equals(qOrderState.getCode());
		if (!nameEquals || !codeEquals) {
			List<ParmItem> items = new ArrayList<ParmItem>(2);
			items.add(new ParmItem("name", orderState.getName(),
					FieldOperator.EQUAL, LogicOperator.AND));
			items.add(new ParmItem("code", orderState.getCode(),
					FieldOperator.EQUAL, LogicOperator.OR));
			List<OrderState> list = orderStateService.findAllByQuery(
					OrderState.class, items);

			if (list == null || list.size() == 0) {
				orderState.setCreateTime(qOrderState.getCreateTime());
				orderState.setCreator(qOrderState.getCreator());
				orderStateService.update(orderState);
			} else {
				this.sendErrorResponse(response, "该类型名称或编码已存在。");
			}
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取订单状态:" + id);
		OrderState orderState = orderStateService.read(id);
		String json = JsonUtils.bean2Json(orderState);
		log.info("订单状态：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除订单状态:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的订单状态。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			orderStateService.delete(Long.parseLong(id));
		}
	}
}
